package cs575.wido.ui;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import java.awt.Component;
import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.FlowLayout;
import javax.swing.JButton;

import cs575.wido.action.LoginAction;

public class LoginWindow extends JFrame {

	private JPanel contentPane;
	private JPanel main_panel;
	private JPanel form_panel;
	private JLabel lblUsername;
	private JTextField txtUsername;
	private JLabel lblPassword;
	private JPasswordField txtPassword;
	private JLabel lblInstruction;
	private Component glue;
	private JLabel lblLogin;
	private JPanel button_panel;
	private JButton btnLogin;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginWindow frame = new LoginWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginWindow() {
		setTitle("WiDo");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		main_panel = new JPanel();
		contentPane.add(main_panel, BorderLayout.NORTH);
		main_panel.setLayout(new BorderLayout(0, 0));
		
		lblLogin = new JLabel("Login");
		lblLogin.setFont(new Font("Tahoma", Font.PLAIN, 20));
		main_panel.add(lblLogin, BorderLayout.NORTH);
		
		form_panel = new JPanel();
		main_panel.add(form_panel, BorderLayout.CENTER);
		form_panel.setLayout(new GridLayout(0, 2, 0, 0));
		
		lblInstruction = new JLabel("Enter a user name and password");
		form_panel.add(lblInstruction);
		
		glue = Box.createGlue();
		form_panel.add(glue);
		
		lblUsername = new JLabel("User name:");
		form_panel.add(lblUsername);
		
		txtUsername = new JTextField();
		form_panel.add(txtUsername);
		txtUsername.setColumns(10);
		
		lblPassword = new JLabel("Password:");
		form_panel.add(lblPassword);
		
		txtPassword = new JPasswordField();
		form_panel.add(txtPassword);
		txtPassword.setColumns(10);
		
		button_panel = new JPanel();
		main_panel.add(button_panel, BorderLayout.SOUTH);
		button_panel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
		
		btnLogin = new JButton("Login");
		getRootPane().setDefaultButton(btnLogin);
		button_panel.add(btnLogin);
		
		buildAction();
		
		pack();
	}
	
	private void buildAction() {
		btnLogin.setAction(new LoginAction(this));
	}
	
	public String getUsername() {
		return this.txtUsername.getText();
	}
	
	public String getPassword() {
		return new String(this.txtPassword.getPassword());
	}

}
